<?php
class Categories extends CI_Model {
	var $database;
	public function __construct() {
        parent::__construct();
		$this->database = $this->db->conn_id;
		
	}
	/**
     * @author: Nguyen Thanh Tung
     * @description: get ID
     * @param integer: $intCategory
     * @return row
    */
	public function getId($intId)
	{	
		$stmt = $this->database->prepare('CALL categories_get(?)');
		$stmt->bindParam(1,$intId,PDO::PARAM_INT,11);
		$stmt->execute();
		return $stmt->fetch();
	}
	/**
     * @author: Nguyen Thanh Tung
     * @description: get all child category
     * @param integer: $intParentId
     * @return array
    */
	public function getAllChild($intParentId){
		$stmt = $this->database->prepare('CALL categories_getall_child(?)');
		$stmt->bindParam(1,$intParentId,PDO::PARAM_INT);
		$stmt->execute();
		return $stmt->fetchAll();
		
	}
	
	/**
     * @author: Nguyen Thanh Tung
     * @description: get all parent category
     * @param integer: $intType
     * @return array
    */
	public function getAllParentId($intType){
		$stmt = $this->database->prepare('CALL categories_getall_parent(?)');
		$stmt->bindParam(1,$intParentId,PDO::PARAM_INT);
		$stmt->execute();
		return $stmt->fetchAll();
		
	}
	/**
     * @author: Nguyen Thanh Tung
     * @description: get all category
     * @return array
    */
    public function getAll($intAdminType, $intType,$intLang,$intStoreid,$intStatus,$strStartdate,
									$strEndday,$intOffset,$intLimit, &$intTotal=0) {
		$stmt = $this->database->prepare('call categories_getall_a(?,?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intAdminType,   PDO::PARAM_INT);
		$stmt->bindParam(2, $intType,    PDO::PARAM_INT);
		$stmt->bindParam(3, $intLang,    PDO::PARAM_INT);
		$stmt->bindParam(4, $intStoreid,    PDO::PARAM_INT);
		$stmt->bindParam(5, $intStatus,    PDO::PARAM_INT, 4);
		$stmt->bindParam(6, $strStartdate,    PDO::PARAM_STR);
		$stmt->bindParam(7, $strEndday,    PDO::PARAM_STR);
		$stmt->bindParam(8, $intOffset,    PDO::PARAM_INT);
		$stmt->bindParam(9, $intLimit,    PDO::PARAM_INT, 4);
        $stmt->execute();
		$arrResult 	= $stmt->fetchAll();		
		$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;	
		return isset($arrResult) ? $arrResult : null;
	}	
	/**
     * @author: Nguyen Thanh Tung
     * @description: add item category
     * @return row
    */
	public function add($intAdminId, $intStoreid, $strName,$strSlug,$strShort,$strImage,
											$strImage2,$intParentId, $intIseq,$intType,$strTag,$intLang=null) {		
		$stmt = $this->database->prepare('call categories_add(?,?,?,?,?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intAdminId, 	PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $intStoreid, 	PDO::PARAM_INT);
		$stmt->bindParam(3, $strName, 	PDO::PARAM_STR,150);
		$stmt->bindParam(4, $strSlug, 	PDO::PARAM_STR,255);
		$stmt->bindParam(5, $strShort, 	PDO::PARAM_STR,250);
		$stmt->bindParam(6, $strImage, 	PDO::PARAM_STR,150);
		$stmt->bindParam(7, $strImage2, 	PDO::PARAM_STR,150);
		$stmt->bindParam(8, $intParentId, 	PDO::PARAM_INT);
		$stmt->bindParam(9, $intIseq, 	PDO::PARAM_INT);
		$stmt->bindParam(10, $intType, 	PDO::PARAM_INT, 4);
		$stmt->bindParam(11, $strTag, 	PDO::PARAM_STR,100);
		$stmt->bindParam(12, $intLang, 	PDO::PARAM_INT);
		
        $stmt->execute();
		$arrResult = $stmt->fetch();					
		return isset($arrResult['errCode']) ? $arrResult['errCode'] : -1;		
	}
	/**
     * @author: Nguyen Thanh Tung
     * @description: update item category
     * @return row
    */
	public function update($intAdminId, $intCatId, $intStoreid,$strName,$strSlug,$strShort,
											$strIcon,$strIconAct, $intIseg,$intType,$strTag,$intLang=null) {
		$stmt = $this->database->prepare('call categories_update(?,?,?,?,?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intAdminId, 	PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $intCatId, 	PDO::PARAM_INT);
		$stmt->bindParam(3, $intStoreid, 	PDO::PARAM_INT);
		$stmt->bindParam(4, $strName, 	PDO::PARAM_STR,150);
		$stmt->bindParam(5, $strSlug, 	PDO::PARAM_STR,255);
		$stmt->bindParam(6, $strShort, 	PDO::PARAM_STR,150);
		$stmt->bindParam(7, $strIcon, 	PDO::PARAM_STR,150);
		$stmt->bindParam(8, $strIconAct, 	PDO::PARAM_STR,150);
		$stmt->bindParam(9, $intIseg, 	PDO::PARAM_INT);
		$stmt->bindParam(10, $intType, 	PDO::PARAM_INT);
		$stmt->bindParam(11, $strTag, 	PDO::PARAM_STR,100);
		$stmt->bindParam(12, $intLang, 	PDO::PARAM_INT);
		$stmt->execute();        
	}	
	/**
     * @author: Nguyen Thanh Tung
     * @description: update status category
	 * @param integer:$intAdminid
	 * @param integer:$intCatId
	 * @param integer:$intStatus
	 * @param integer:$intPerm
     * @return bool
    */
	public function updateStatus($intAdminid,$intCatId,$intStatus,$intPerm){
		$stmt = $this->database->prepare('call categories_update_status(?,?,?,?)');
		$stmt->bindParam(1, $intAdminId, 	PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $intCatId, 	PDO::PARAM_INT,11);
		$stmt->bindParam(3, $intStatus, 	PDO::PARAM_INT,4);
		$stmt->bindParam(4, $intPerm, 	PDO::PARAM_INT,4);
		if($stmt->execute())
			return TRUE;
		else
			return FALSE;
		
	}

}